//
//  HSDrawerController.swift
//  HomeSchoolDemo
//
//  Created by Mac on 16/9/27.
//  Copyright © 2016年 chendong. All rights reserved.
//

import UIKit

class HSDrawerController: UIViewController {
    //创建左边视图控制器
    let leftViewVc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "HSLeftViewController")
    //创建标签视图控制器
    let tabBarVc = HSTabBarController()
    //动画时长
    let animationTime:TimeInterval = 0.5
    //标签视图的动画移动最大tx值
    let barOffset = screenW * 0.842
    //左边视图的动画移动最大tx值
    let leftViewOffset = screenW / 2
    //计算两个视图的移动比例
    var offsetScale:CGFloat{
        return leftViewOffset / barOffset
    }
    //记录第一次触摸点
    var currentP:CGPoint?
    
    //懒加载遮盖视图
    lazy var coverview:UIButton = {
        //创建按钮
        let btn = UIButton(frame: UIScreen.main.bounds)
        btn.backgroundColor = #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1)
        //监听方法
        btn.addTarget(self, action: #selector(leftAnimation), for: .touchUpInside)
        btn.alpha = 0.1
        return btn
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        //添加手势
        let panGesture = UIPanGestureRecognizer(target: self, action: #selector(panGestureFunc(gesture:)))
        self.view.addGestureRecognizer(panGesture)
    }
    
    //手势回调方法
    @objc fileprivate func panGestureFunc(gesture:UIPanGestureRecognizer){
        switch gesture.state {
        case .began:
            print("拖动开始")
            //先获得第一次触摸点
            currentP = gesture.location(in: gesture.view)

        case .changed:
            //先获得移动点
            let offset = gesture.translation(in: gesture.view)
            print("拖动中")
            //获得新的移动点
            let newTx = tabBarVc.view.transform.tx + offset.x
            if newTx >= barOffset{
                rightAnimation()
                return
            }
            
            if newTx <= 0 {
                leftAnimation()
                return
            }
            
            let leftNewTx = leftViewVc.view.transform.tx + offset.x * offsetScale
            
            //重新设置视图的位置
            tabBarVc.view.transform = CGAffineTransform(translationX: newTx, y: 0)
            leftViewVc.view.transform = CGAffineTransform(translationX: leftNewTx , y: 0)
            //恢复初始值
            gesture.setTranslation(CGPoint(), in: gesture.view)
        case .ended:
            
            let endP = gesture.location(in: gesture.view)
            if (currentP?.x)! < endP.x {
                rightAnimation()
            }else{
                leftAnimation()
            }
            
            print("结束")
        default:
            break
        }
    }
    
    //右边滑动动画方法
    func rightAnimation() {
        //添加遮盖按钮
        self.tabBarVc.view.addSubview(self.coverview)
        //动画移动视图
        UIView.animate(withDuration: animationTime, animations: {
            self.tabBarVc.view.transform = CGAffineTransform(translationX: self.barOffset, y: 0)
            self.leftViewVc.view.transform = CGAffineTransform(translationX: 0, y: 0)
        }) { (_) in
                print("动画完成")
        }
        
    }
    
    //左边滑动动画方法
    func leftAnimation() {
        UIView.animate(withDuration: animationTime, animations: {
            self.tabBarVc.view.transform = CGAffineTransform(translationX: 0, y: 0)
            self.leftViewVc.view.transform = CGAffineTransform(translationX: -self.leftViewOffset, y: 0)
            }) { (_) in
                print("动画完成")
                //取消遮盖视图
                self.coverview.removeFromSuperview()
        }
    }
    
    func setupUI(){
        view.backgroundColor = UIColor.white
        // 添加抽屉效果子控制器
        // 添加控制器到管理控制器
        addChildViewController(leftViewVc)
        //设置frame
        leftViewVc.view.frame = CGRect(x: 0, y: 0, width: barOffset+3, height: screenH)
        //设置边界阴影
        leftViewVc.view.layer.shadowOffset = CGSize(width: 3, height: 0)
        leftViewVc.view.layer.shadowOpacity = 0.9
        self.leftViewVc.view.transform = CGAffineTransform(translationX: -self.leftViewOffset, y: 0)
        // 同时添加视图
        view.addSubview(leftViewVc.view)
        
        //添加标签控制器
        addChildViewController(tabBarVc)
        //设置frame
        tabBarVc.view.frame = CGRect(x: 0, y: 0, width: screenW, height: screenH)
        view.addSubview(tabBarVc.view)
        
        //获得导航左边按钮的监听事件
        tabBarVc.leftBtnBlock = {()->() in
            print("------------")
            //transform.tx 就是视图改变的X值
            if self.tabBarVc.view.transform.tx == 0{
                self.rightAnimation()
            } else{
                self.leftAnimation()
            }
        }
    }
    
    
    
    
    
    
    
    
    
    
    
    
}
